package com.molin.threadTest.threadSecurety.blockcode;


public class Ticket implements Runnable {
    private Integer tickets = 100;
    private Object lock=new Object();
    public Integer getTickets() {
        return tickets;
    }

    public void setTickets(Integer tickets) {
        this.tickets = tickets;
    }

    @Override
    public void run() {
        while (true) {
            //同步代码块
            synchronized (lock){
                if(tickets==0){break;}
                tickets--;
                System.out.println(Thread.currentThread().getName() + "在售票，还剩下" + tickets + "票");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
